/**
 * 主页面用js
 * 
 * @author baotx
 */
$.namespace("main");

var memuType = null;
var menuItem = null;

main.homeBean = {};
main.tab = {};
main.changePwdPageIndex = null;
/**
 * 界面初始化
 */
main.init = function() {
	main.createHomeBeanI18N(main.homeBean);
	main.initMenuTree();
	
	$('#tabs').addtabs();
    
    $('.user-menu-change-password').on('click',main.showChangePwd);
    $('.user-menu-logout').on('click',main.showExit);
    
    $('.change-password-page').on('click','.btn-default',function(){
    	layer.close(main.changePwdPageIndex);
    	return false;
    });
    $('.change-password-page').on('click','.btn-info',main.changePwd);
    
};

/**
 * 初始化菜单树
 */
main.initMenuTree = function(){
    $.ajax({
        url: $.ctx + "/getMenuTree",
        method:'post',
        async: false,
        success: function(data) {
            var str = "";
            for(var i = 0; i < data.menus.length; i++) {
                str += '<li class="treeview">';
                str += '	<a href="#" style="height:40px">';
                str += '		<i class="glyphicon glyphicon-th-list"></i><span>'+data.menus[i].menuName+'</span><i class="glyphicon glyphicon-chevron-left pull-right"></i>';
                str += '	</a>';
                str += '<ul class="treeview-menu">';
                nodes = data.menus[i].childrens;
                for (var j = 0; j < nodes.length; j++) {
                    var noteText = nodes[j].menuName;
                	var menuCode = nodes[j].menuCode;
                    var url = $.ctx + nodes[j].menuAction;
                    str += $.format('<li><a href="#" addtabs="{0}" url="{1}" title="{2}"><i class="glyphicon glyphicon-hand-right"></i>{3}</a></li>',menuCode,url,noteText,noteText);
                }
                str += "</ul>";
                str += "</li>";
            }
            $('.sidebar-menu').append(str);
        }
    });
};

/**
 * 修改密码
 */
main.showChangePwd = function() {
	$('.change-password-page').removeClass('hide');
	$('.change-password-page #oldPwd').val('');
	$('.change-password-page #pwd').val('');
	$('.change-password-page #newPwd').val('');
	
	main.changePwdPageIndex = layer.open({
		type: 1,
	    title: false, //不显示标题
	    content: $('.change-password-page'), //捕获的元素
	    area: ['350px', 'auto'],
	    cancel: function(index){
	        layer.close(index);
	        this.content.show();
			$('.change-password-page').addClass('hide');
	    }
	});
	
	$('.change-password-page .form-horizontal').validate({
		errorPlacement: function(error, element) {
			layer.tips(error, '#'+element.attr('id'));
		},
		submitHandler: main.changePwd
	});
};

/**
 * 确认退出
 */
main.showExit = function() {
	// 询问框
	layer.confirm('是否确认退出？', {
		btn : [ '确定', '取消' ],
		icon: 3,
		title: '提示'
	}, function() {
		window.location.href = $.ctx + "/logout";
	});
};

main.changePwd = function() {
	if (!isEmpty($("#oldPwd").val())) {
		var oldPwd=$("#oldPwd").val();
	}else{
		layer.shortmsg("当前密码不能为空！");
		return;
	}
	if (!isEmpty($("#pwd").val())) {
		var pwd=$("#pwd").val();
			
		}else{
			layer.shortmsg("新密码不能为空！");
			return;
		}
	if (!isEmpty($("#newPwd").val())) {
		var newPwd=$("#newPwd").val();
	    }else{
			layer.shortmsg("确认新密码不能为空！");
			return;
	    }
	if (pwd!=newPwd) {
		layer.shortmsg("确认新密码与新密码不一致，请重新输入！");
		return;
    }
		$.ajax({
			type : 'post',
			url : $.ctx + '/pwdChange',
			data :  $.param({
				"oldPassword": oldPwd,
				"newPassword":pwd
			}),
			success : function(result) {
				if (result=="1") {
					layer.success("密码修改成功！");
					layer.close(main.changePwdPageIndex);
				} else if(result=="0"){
					layer.warning("旧密码输入错误！");
				}else{
					layer.warning("密码修改失败！");
				}
//			},
//			error:function(e){
//				layer.error("密码修改异常！");
			}
		});
	
};

//===============华丽的分割线==============
main.tick = function(){
    var today;
    today = new Date();
    document.getElementById("localtime").innerHTML = main.showLocale(today);
    window.setTimeout("main.tick()", 1000);
};
